<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <!-- <el-form-item label="师傅" prop="staffId">
        <el-input
          v-model="queryParams.staffId"
          placeholder="请输入师傅"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="大厦" prop="buildingId">
        <el-select v-model="queryParams.buildingId" placeholder="请选择大厦" clearable>
          <el-option
            v-for="(label, key) in buildingMap"
            :key="key"
            :label="label"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择商户" clearable>
          <el-option
            v-for="(label, key) in supplierMap"
            :key="key"
            :label="label"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务日期" prop="serveDay">
        <el-date-picker
          v-model="queryParams.serveDay"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选订单状态" clearable>
            <el-option
              v-for="dict in dict.type.lifeservice_orderstatus"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="订单时间" prop="time">
        <el-date-picker
          v-model="queryParams.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['console:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="id" />
      <el-table-column label="大厦名称" align="center" prop="buildingName" />
      <el-table-column label="客户姓名" align="center" prop="guestName" />
      <el-table-column label="客户电话" align="center" prop="guestPhone" />
      <el-table-column label="客户备注" align="center" prop="guestRemark" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="订单评分" align="center" prop="score" />
      <el-table-column label="订单状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lifeservice_orderstatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="服务时间" align="center" prop="serveTime" min-width="125">
        <template slot-scope="scope">
          <div>{{ scope.row.serveDay }} {{ scope.row.serveWeek }}</div>
          <div>{{ scope.row.workTime }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.status == 1||scope.row.status == 2"
            size="mini"
            type="text"
            :disabled="scope.row.status == 2"
            icon="el-icon-finished"
            @click="orderDeclaration(scope.row)"
            v-hasPermi="['console:order:declaration']"
          >{{scope.row.status == 2?'已核销':'核销'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Life员工站订单对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户id" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="师傅id" prop="staffId">
          <el-input v-model="form.staffId" placeholder="请输入师傅id" />
        </el-form-item>
        <el-form-item label="服务时间班次1-x,表示该客户预约了第几个班次的服务" prop="serviceClass">
          <el-input v-model="form.serviceClass" placeholder="请输入服务时间班次1-x,表示该客户预约了第几个班次的服务" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, addOrder, updateOrder, orderDeclaration} from "@/api/console/order";
import { getDictMap } from "@/api/console/info";

export default {
  name: "Order",
  dicts: ['lifeservice_orderstatus'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Life员工站订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingId: null,
        supplierId: null,
        serveDay: null,
        staffId: null,
        status: null,
        serviceClass: null,
        time: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        staffId: [
          { required: true, message: "师傅id不能为空", trigger: "blur" }
        ],
      },
      // 大厦列表
      buildingMap: {},
      // 商户列表
      supplierMap: {}
    };
  },
  created() {
    this.getList();
    this.getDictMap();
  },
  methods: {
    /** 客服务地址 */
    getDictMap() {
      getDictMap({}).then(response => {
        this.supplierMap = (response.data || {}).supplierMap || {};
        this.buildingMap = (response.data || {}).buildingMap || {};
      });
    },
    /** 查询Life员工站订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
      }).finally(res=>{
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierId: null,
        staffId: null,
        createTime: null,
        updateTime: null,
        status: null,
        serviceClass: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Life员工站订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改Life员工站订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 按钮操作 */
    orderDeclaration(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认核销订单ID为"' + ids + '"的数据项？').then(function() {
        return orderDeclaration({id:ids,status:2});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("核销成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('console/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
